package chain.general;

import java.util.logging.Level;

/**
 * @Author：LJ
 * @Description：
 * @Date: 2017/11/30
 * @Modified By:
 */
public abstract class Handler {
    private Handler nextHandler;

    //对请求做出自己出来还是传递给下一个Handler处理
    public final HandlerResponse handleRequest(HandlerRequest handlerRequest) {
        HandlerResponse response = null;
        if (getHandlerLevel().equals(handlerRequest.getRequestLevel())) {
            response = handle();
        } else if (nextHandler != null) {
            response = nextHandler.handleRequest(handlerRequest);
        }
        return response;
    }

    //设置下一个处理者是谁
    public void setNextHandler(Handler nextHandler) {
        this.nextHandler = nextHandler;
    }

    //每个处理者都有一个处理级别,主要作用就是标识它是否能处理这次的请求
    protected abstract Level getHandlerLevel();

    //处理请求
    protected abstract HandlerResponse handle();

}
